using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Blople.GameComponents
{
    public class Blop : Microsoft.Xna.Framework.DrawableGameComponent
    {
        protected Rectangle screenBounds;

        protected Texture2D texture;

        protected Vector2 position;
        protected Vector2 velocity;
        protected float direction;

        protected const int WIDTH = 80;
        protected const int HEIGHT = 80;

        protected Rectangle frame;

        enum Colors { Red, Green, Blue, Yellow };

        Colors color;

        enum StatusBlop { Active, Passive };

        StatusBlop status;

        protected Random random;

        protected Blop adjacentBlop;

        public Blop(Game game, Texture2D texture, int level) : base(game)
        {
            screenBounds = new Rectangle(0, 0, Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height);
            this.texture = texture;
            random = new Random(System.Environment.TickCount + this.GetHashCode());
            color = (Colors)random.Next(0, level + 1);
            adjacentBlop = null;
            CreateBlopFrame();
            SetStartPosition();
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            if (status == StatusBlop.Active)
            {
                position += velocity;
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            sBatch.Draw(texture, position, frame, Color.White, 0.0f, new Vector2(frame.Width / 2, frame.Height / 2), 1.0f, SpriteEffects.None, 0.3f);
            base.Draw(gameTime);
        }

        public void CreateBlopFrame()
        {
            frame = new Rectangle(0, (int)color * HEIGHT, WIDTH, HEIGHT);
        }

        public void SetStartPosition() 
        {
            position = new Vector2(250, 650);
            status = StatusBlop.Passive;
        }

        public void ShootBlop(float direction)
        {
            if (status != StatusBlop.Active) 
            {
                status = StatusBlop.Active;
                velocity.X = (float)(Math.Sin(direction) * 5.0f);
                velocity.Y = -(float)(Math.Cos(direction) * 5.0f);
            }
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        public Blop AdjacentBlop
        {
            get { return adjacentBlop; }
            set { adjacentBlop = value; }
        }

        public void SwitchStatus()
        {
            if (status == StatusBlop.Active) {
                status = StatusBlop.Passive;
            } else if (status == StatusBlop.Passive) {
                status = StatusBlop.Active;
            }
        }

        public int getColor()
        {
            return (int)color;
        }
    }
}
